package com.atguigu.gmall.user.service.impl;

import com.atguigu.gmall.common.util.MD5;
import com.atguigu.gmall.model.user.UserInfo;
import com.atguigu.gmall.user.mapper.UserInfoMapper;
import com.atguigu.gmall.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class UserServiceImpl implements UserService {

    //  需要mapper user_info
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public UserInfo login(UserInfo userInfo) {
        //  实现：
        //  select * from user_info where login_name = ? and passwd = ?;
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("login_name",userInfo.getLoginName()); // atguigu
        //  怎么做： 将页面提交过来的密码，进行加密处理. md5 加密过程. 111111 ---MD5---> 96e79218965eb72c92a549dd5a330112
        String newPwd = DigestUtils.md5DigestAsHex(userInfo.getPasswd().getBytes());
        // String encrypt = MD5.encrypt(userInfo.getPasswd());
        userInfoQueryWrapper.eq("passwd",newPwd);

        //  查询数据库获取到返回结果。
        UserInfo info = userInfoMapper.selectOne(userInfoQueryWrapper);
        //  判断
        if(info!=null){
            return info;
        }
        //  默认返回null；
        return null;
    }
}
